#! -*- coding:utf-8 -*-

import socket

socket_timeout = 0.1


def tcp_scan(one_ip, port) -> bool:
    """
    利用socket 发送tcp包，探测端口存活率

    :param one_ip: 1.1.1.1  type is string
    :param port: 22 type is int

    """
    try:
        s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)  # 基于简单的TCP扫描
        s.settimeout(socket_timeout)
        c = s.connect_ex((one_ip, port))
        if c == 0:
            print("%s:%s is open" % (one_ip, port))
            return True
        else:
            # print "%s:%s is not open" % (ip,port)
            return False
    except Exception as e:
        print(e)
    finally:
        s.close()
